package com.example.springboothello.listener;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class MyEventListener {


    //通过ApplicationEvent携带参数，这样可以通过判断处理不同的事件类型
    @EventListener({OrderEvent.class, UserEvent.class})
    @Order(0) // 值越小优先级越高
    public void receiveOrderEvent(ApplicationEvent event) {
        System.out.printf("监听到定义事件：%s", event.getSource()) ;
    }
    @EventListener
    @Order(1)
    //@Async 异步事件监听1
    public void receiveOrderEvent(OrderEvent event) {
        System.out.printf("监听到定义事件：%s", event.getOrder().toString()) ;
    }
    // 事件监听; 过滤只有User对象的type属性为1时才会触发事件
    @EventListener(condition = "#event.user.type == 1")
    public void receiveOrderEvent(UserEvent event) {
        System.out.printf("监听到定义事件：%s", event.getUser()) ;
    }
}
